local extension = Package:new("nya_ex")
extension.extensionName = "wild_collection"
Fk:loadTranslationTable{
  ["nya_ex"] = "喵扩",
  ["nya"] = "喵",
  }
local U = require "packages/utility/utility"
local doumao = fk.CreateTriggerSkill{
  name = "nya_doumao",
  anim_type = "special",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      (player.phase == Player.Start or player.phase == Player.Finish) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    if player.phase == Player.Start then
      local to, card =  player.room:askForChooseCardAndPlayers(player, table.map(player.room:getOtherPlayers(player), function(p)
        return p.id end), 1, 1, ".", "#nya_doumao-choose", self.name, true)
      if #to > 0 and card then
        self.cost_data = {to[1], card}
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player.phase == Player.Start then
      room:throwCard(self.cost_data[2], self.name, player, player)
      room:handleAddLoseSkills(player, "-nya_doumao", nil, true, false)
      local to = room:getPlayerById(self.cost_data[1])
      if not to.dead then
        to:drawCards(1, self.name)
        if not to:hasSkill(self) then
          room:handleAddLoseSkills(to, "nya_doumao", nil, true, false)
        end
      end
    else
      room:askForDiscard(player, 1, 1, true, self.name, false)
    end
  end,
}
Fk:loadTranslationTable{
  ["nya_doumao"] = "逗猫",
  [":nya_doumao"] = "准备阶段，你可以弃置一张牌并失去“逗猫”，令一名其他角色摸一张牌，然后若其没有“逗猫”，则其获得之。结束阶段，若你有“逗猫”，你弃置一张牌。",
  ["#nya_doumao-choose"] = "逗猫：你可以弃置一张牌并失去“逗猫”，然后令一名其他角色摸一张牌，然后若其没有“逗猫”，其获得之",
}
local mayunlu = General(extension, "nya_ex__mayunlu", "shu", 4, 4, General.Female)
local yuma = fk.CreateTriggerSkill{
  name = "nya_yuma",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            return (Fk:getCardById(info.cardId).sub_type == Card.SubtypeOffensiveRide)
            or (Fk:getCardById(info.cardId).sub_type == Card.SubtypeDefensiveRide)
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local i = 0
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip then
            i = i + 1
          end
        end
      end
    end
    self.cancel_cost = false
    for _ = 1, i do
      if self.cancel_cost or not player:hasSkill(self) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 2, self.name)
  end,
}
local yuma_distance = fk.CreateDistanceSkill{
  name = "#nya_yuma_distance",
  frequency = Skill.Compulsory,
  correct_func = function(self, from, to)
    if from:hasSkill(self) then
      if not to:hasSkill("nya_doumao") then
        return -1
      end
    end
  end,
  fixed_func = function(self, from, to)
    if from:hasSkill(self) then
      if to:hasSkill("nya_doumao") then
        return 1
      end
    end
  end,
}
local fengpo = fk.CreateTriggerSkill{
  name = "nya_fengpo",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) or target ~= player then return end
    if player:getMark("fengpo_slash-turn") > 0 and player:getMark("fengpo_duel-turn") > 0 and player:getMark("@@fengpo_skipcheck") == 0 then return end
    if data.card and #U.getActualUseTargets(player.room, data, event) == 1 then
      return (data.card.trueName == "slash" and (player:getMark("fengpo_slash-turn") == 0 or player:getMark("@@fengpo_skipcheck") > 0)) 
      or (data.card.name == "duel" and (player:getMark("fengpo_duel-turn") == 0 or player:getMark("@@fengpo_skipcheck") > 0))
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(U.getActualUseTargets(room, data, event)[1])
    local cids = to:getCardIds(Player.Hand)

    if data.card.name == "duel" then
      room:setPlayerMark(player, "fengpo_duel-turn", 1)
    else
      room:setPlayerMark(player, "fengpo_slash-turn", 1)
    end
    local n = #table.filter(cids, function(id) 
      return Fk:getCardById(id).color == Card.Red
    end)
    if player:getMark("@@fengpo_skipcheck") > 0 then
      n = player:getMark("fengpo_lastnum")
    end
    n = math.min(n, 4)
    local choice = "nya_fengpo_draw:::" .. n 
    local result
    if not player:hasSkill("nya_doumao") then
      result = room:askForCustomDialog(player, self.name,
        "packages/utility/qml/ChooseCardsAndChoiceBox.qml", {
          cids,
          {"nya_fengpo_draw:::" .. n, "nya_fengpo_damage:::" .. n},
          "#nya_fengpo-choose::" .. data.to,
          {}, 0, 0,
        })
      if result ~= "" then
        local reply = json.decode(result)
        choice = reply.choice
      end
    else
      choice = room:askForChoice(player, {"nya_fengpo_draw:::" .. n, "nya_fengpo_damage:::" .. n}, self.name, "#nya_fengpo-choose::" .. data.to, false)
    end

    if choice:startsWith("nya_fengpo_draw") then
      player:drawCards(n, self.name)
      data.additionalDamage = (data.additionalDamage or 0) + 1
    else
      player:drawCards(1, self.name)
      data.additionalDamage = (data.additionalDamage or 0) + n
    end
    if player:getMark("@@fengpo_skipcheck" == 0) then
      room:setPlayerMark(player, "fengpo_lastnum", n)
    end
  end,
  refresh_events = {fk.Death},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damage and data.damage.from == player and target:hasSkill("nya_doumao", false, true)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@fengpo_skipcheck", 1)
  end,
}
yuma:addRelatedSkill(yuma_distance)
mayunlu:addSkill(yuma)
mayunlu:addSkill(fengpo)
mayunlu:addSkill(doumao)
Fk:loadTranslationTable{
  ["nya_ex__mayunlu"] = "喵马云騄",
  ["#nya_ex__mayunlu"] = "喵胆琴心",
  ["nya_yuma"] = "驭猫",
  [":nya_yuma"] = "锁定技，你计算与有/没有“逗猫”的角色的距离视为1/-1。当你失去装备区里的一张坐骑牌后，你摸两张牌。",
  ["nya_fengpo"] = "凤魄",
  [":nya_fengpo"] = "每回合各限一次，当你使用【杀】或【决斗】指定唯一目标后，你可以选择一项（若你没有“逗猫”，改为观看其手牌并选择一项）"..
  ":摸X张牌，此伤害+1；摸一张牌，此伤害+X（X为目标角色的红色手牌数且至多为4）。当你杀死有“逗猫”的角色后，你修改凤魄（删除“每回合各限一次”，将X改为上次的数值）。",
  ["nya_fengpo_draw"] = "摸%arg张牌并增加1点伤害",
  ["nya_fengpo_damage"] = "摸一张牌并增加%arg点伤害",
  ["#nya_fengpo-choose"] = "凤魄：对%dest选择一项",
  ["@@fengpo_skipcheck"] = "凤魄",
}
return extension